package token

import (
	"bufio"
	"gitee.com/kaylee595/gkaylee/kerr"
	"io"
)

type Decoder struct {
	r *bufio.Scanner
}

func NewDecoder(r io.Reader) *Decoder {
	return &Decoder{r: bufio.NewScanner(r)}
}

func (d *Decoder) ReadLine() (Line, error) {
	for {
		if !d.r.Scan() {
			err := d.r.Err()
			if err != nil {
				return nil, kerr.WrapTraceStack(err)
			}
			return nil, io.EOF
		}
		l := d.r.Bytes()
		if len(l) == 0 {
			continue
		}
		return l, nil
	}
}
